package com.huluobo.a2110mvpimdemo.demo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by LC on 2023/12/14.
 */
public class MyView extends View {

    private int progress;//绘制的进度 0-360
    private String timeString;

    //在Java里面new的时候一般用到这个
    public MyView(Context context) {
        super(context);
//        TextView textView = new TextView(getContext());
    }

    //AttributeSet 属性集合 一般用在xml文件中使用
    //在xml中声明的属性,都会出现在AttributeSet中
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
//        start(5);
    }


    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private Paint paint;//画笔
    private Paint textPaint;

    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.RED);//画笔设置颜色
        paint.setStyle(Paint.Style.STROKE);//设置绘制线
        paint.setStrokeWidth(5);//设置线的宽度
//        paint.setTextSize(100);

        textPaint = new Paint();
        textPaint.setColor(Color.RED);
        textPaint.setTextSize(200);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //给一个圆限定一个范围,距离LTRB都是距离顶部或者左边的距离,所以这个矩形是个边长500*500的正方形
        //其中的圆就是直径500 半径250圆
        RectF rectF = new RectF(100, 100, 600, 600);

        //参数: 范围 起始角度 终点角度 是否使用圆心 画笔
        canvas.drawArc(rectF, 270, progress, false, paint);
        canvas.drawText(timeString, 300, 420, textPaint);
    }

    public void start(int time) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
//        valueAnimator.setInterpolator(new BounceInterpolator());
        valueAnimator.addUpdateListener(animation -> {
            progress = (int) animation.getAnimatedValue();//获取更新的值
            invalidate();//view刷新自身的方法,更新圆画线的进度
        });
        valueAnimator.setDuration(time * 1000L);
        valueAnimator.start();

        ValueAnimator valueAnimatorText = ValueAnimator.ofInt(time, 0);
        valueAnimatorText.addUpdateListener(animation -> {
            timeString = String.valueOf(valueAnimatorText.getAnimatedValue());
            invalidate();
        });
        valueAnimatorText.setDuration(time * 1000L);
        valueAnimatorText.start();

        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //4.在对应的位置,调用接口的方法
                onAnimEndListener.onAnimEnd();
            }
        });
    }

    //接口回调
    //1.创建一个接口
    public interface OnAnimEndListener {
        void onAnimEnd();
    }

    //2.在当前类声明接口
    private OnAnimEndListener onAnimEndListener;

    //3.给接口生成一个set方法,对外暴露这个方法
    public void setOnAnimEndListener(OnAnimEndListener onAnimEndListener) {
        this.onAnimEndListener = onAnimEndListener;
    }
}
